package com.yc.supermarket.web.controller;

import com.yc.supermarket.entity.Sales;
import com.yc.supermarket.entity.Transaction;
import com.yc.supermarket.service.IUnlineBuyService;
import com.yc.supermarket.service.impl.UnlineServiceImpl;
import org.apache.ibatis.annotations.Select;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/buys")
public class UnlinebuyController extends  BaseController{
    private IUnlineBuyService service =new UnlineServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String op = req.getParameter("op");
        if ("findprice".equals(op)) {
             doFindPrice(req,resp);
        }else if("founds".equals(op)){
             doFounds(req,resp);
        }else if("foundtails".equals(op)){
             doFoundTails(req,resp);
        }else if("find".equals(op)){
            doFind(req,resp);
        }else if("submit".equals(op)) {
            doSubmint(req,resp);
        }else if("modify".equals(op)){
            doModify(req,resp);
        }else if("over".equals(op)){
            doOver(req,resp);
        }else{
            resp.getWriter().print("WeiGe: no such Methond ...");
        }
    }

    protected void doFindPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String goodsid=req.getParameter("goid");
       int myid=Integer.parseInt(goodsid);
       Double a=service.findprice(myid);
       printToJson(resp,a);
    }

    protected void doFounds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       int a=service.founds();
       printToJson(resp,a);
    }

    protected void  doFoundTails(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Sales orderTail=parseRequest(req,Sales.class);
        int a=service.foundtails(orderTail);
        printToJson(resp,a);
    }

    protected void doFind(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Sales orderTail=parseRequest(req,Sales.class);
        Map<String,Object> map=service.finds(orderTail);
        printToJson(resp,map);
    }

    protected void doSubmint(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Transaction order=parseRequest(req, Transaction.class);
        System.out.println("thusd:"+order);
       int a=service.submit(order);
        System.out.println("thea:"+a);
        printToJson(resp,a);
    }



    protected void doOver(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Sales orderTail=parseRequest(req,Sales.class);
        int a=service.doover(orderTail);
        System.out.println("aaaa:"+a);
        printToJson(resp,a);
    }


    protected void doModify(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Sales orderTail=parseRequest(req,Sales.class);
        System.out.println("theoerd:"+orderTail);
        int a=service.modify(orderTail);
        System.out.println("theyq:"+a);
        printToJson(resp,a);
    }


}
